This is the very first exercise of this course. It’s not too hard, its purpose is instead to get used to our exercise format - this HTML file, for instance - and more importantly, to get used to the tidyverse world.
Consequently, this exercise is really short and just let you play around with some pipes and tibbles. It’s a mini-exercise!
First things first: To work with the ‘tidyverse’, we have to have access to its packages.
tidyverse library.
tidyverse library has not been installed yet, you can install it with the command install.packages("tidyverse").
After successfully loading the tidyverse library we turn to the magic world of pipes. Remember, pipes are a convenient way to disentangle nested R functions and to write cleaner R code. First, have a look at the code in the following block:
mean(sqrt(as.numeric(read.csv2("../data/titanic/titanic.csv", sep = ",")$Fare)))
Using the commands in such a way is not very clear, isn’t it? You have already learned that pipes provide a straightforward approach to navigate this issue.
.$col_name.
As we’ve learned, tidyverse is not all about pipes, it’s about tidy data. The default data format to have access to tidy data in tidyverse is the tibble format. In the previous task, you have already imported the titanic data, but it’s in the standard data.frame format.
titanic dataset and convert it immediatly to a tibble.
base-R’s read.csv2() is your friend. Also, you may want to do it all at once in one pipe.
Now, look at the following data.frame. It’s been created with the standard base-R tools. tidyverse also provides a feature, the tribble() command, to create small data tables as tibbles from scratch.
## day amount_coffee words_written
## 1 1 2 245
## 2 2 5 691
## 3 3 1 10
## 4 4 8 2100
## 5 5 4 490
tribble()-function to directly rebuild the data frame as a tibble.
~.